<?php
/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

JFormHelper::loadFieldClass('text');

class JFormFieldXmlTypeElement extends JFormFieldText {

    public $type = 'XmlTypeElement';

    protected function getInput() {
        // Initialize some field attributes.
        $sSize = $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : '';

        $sMaxLength = $this->element['maxlength'] ? ' maxlength="' . (int) $this->element['maxlength'] . '"' : '';

        $sClass = $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';

        $sReadonly = ' readonly="readonly"';

        $sDisabled = ' disabled="disabled"';

        // Initialize JavaScript field attributes.
        $sOnchange = $this->element['onchange'] ? ' onchange="' . (string) $this->element['onchange'] . '"' : '';

        // Change the display XML Type.
        $oDatabase = JFactory::getDbo();
        $oQuery = $oDatabase->getQuery(true);

        $oQuery->select('axt.title');
        $oQuery->from('#__auto_xml_type AS axt');
        $oQuery->where('axt.id = ' . (int) $this->value);

        $oDatabase->setQuery($oQuery);

        try {
            $oXMLType = $oDatabase->loadObject();
        } catch (RuntimeException $oException) {
            JError::raiseWarning(500, $oException->getMessage());
        }

        $sXMLTypeName = '';

        if ($oXMLType)
            $sXMLTypeName = $oXMLType->title;
        else
            $sXMLTypeName = JText::_('COM_AUTOGENERATOR_XML_ELEMENT_FIELD_XML_TYPE_ID_NOT_VALID');

        $sHtml = '<input type="text" name="' . $this->name . '" id="' . $this->id . '"' . ' value="' . htmlspecialchars($sXMLTypeName, ENT_COMPAT, 'UTF-8') . '"' . $sClass . $sSize . $sDisabled . $sReadonly . $sOnchange . $sMaxLength . '/>';
        $sHtml .= '<input type="hidden" name="' . $this->name . '" value="' . htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8') . '" />';

        return $sHtml;
    }

}
